<?php
define('FR_ROOT', '/home/ronnie/document/sites/tiaoshei/');
define('SITE_TYPE', 'www');
define('TMP_ROOT', FR_ROOT.'tmp');
define('RUN_ROOT', FR_ROOT.'runroot');
require_once FR_ROOT.'HapN/core/Conf.php';
require_once dirname(__DIR__).'/VoteExport.php';

Conf::load(FR_ROOT.'conf/hapn.conf.php');

require_once FR_ROOT.'lib/db/Db.php';
Db::init(Conf::get('db.conf'));

require_once FR_ROOT.'HapN/core/Logger.php';

class VoteTest extends PHPUnit_Framework_TestCase
{
    /**
     * 'vote_summary',
     *  'vote_cover',
     *  'vote_allow_num',
     *  'user_id',
     *  'access_type',
     *  'access_begin',
     *  'access_stop',
     *  'access_token',
     *  'create_ip',
     * @return [type] [description]
     */
    public function provider()
    {
        $datas = array();
        $types = array(
            VoteConst::VOTE_TYPE_IMAGE,
            VoteConst::VOTE_TYPE_TEXT,
            VoteConst::VOTE_TYPE_URL,
        );
        $access = array(
            VoteConst::ACCESS_TYPE_PUBLIC,
            VoteConst::ACCESS_TYPE_TOKEN,
            VoteConst::ACCESS_TYPE_IP,
        );
        for($i = 0; $i < 4; $i++) {
            $idx = rand(0, 2);
            $ac = rand(0, 2);
            $datas[] = array(
                $types[$idx],
                'vote title '. $i,
                array(
                    'user_id' => 1000000,
                    'create_ip'     => '113.44.53.'.rand(1, 255),
                    'access_type'   => $access[$ac],
                ),
            );
        }
        return $datas;
    }

    /**
     * @dataProvider provider
     * @return [type] [description]
     */
   function testAddVote()
    {
        $voteExport = new VoteExport();
        $args = func_get_args();
        $voteId = $voteExport->addVote($args[0], $args[1], $args[2]);
        $this->assertArrayHasKey('vote_guid', $voteId);
    }

    public function idProvider()
    {
        return array(
            array(10010),
            array(10100),
            array(10201),
            array('8e5c5e848e8e18338049aafb5eb63690f30a7167'),
            array('cc024b0ceea36c3f1f827fbb7882b0be1cd8efe9'),
            array('f9a4f2778a33c1a2c876f91fd895b6fa81606a846'),
            array('sdf34'),
        );
    }

    /**
     * @dataProvider idProvider
     * @return [type]         [description]
     */
    function testGetVote()
    {
        $voteExport = new VoteExport();
        $vote = $voteExport->getVote(func_get_arg(0));
        $this->assertNotEmpty($vote);
    }

    function testDependAddVote()
    {
        $data = array(
            2,
            'vote title depend',
            array(
                'user_id' => 1000000,
                'create_ip'     => '113.44.53.'.rand(1, 255),
                'access_type'   => 2,
            ),
        );
        $voteExport = new VoteExport();
        $voteId = $voteExport->addVote($data[0], $data[1], $data[2]);
        $this->assertArrayHasKey('vote_guid', $voteId);
        return $voteId;
    }

    /**
     * @depends testDependAddVote
     */
    function testDependGetVote($ids)
    {
        $voteExport = new VoteExport();
        $vote = $voteExport->getVote($ids['vote_id']);
        $this->assertNotEmpty($vote);

        $vote = $voteExport->getVote($ids['vote_guid']);
        $this->assertNotEmpty($vote);
        return $vote;
    }

    /**
     * @depends testDependGetVote
     */
    function testDependUpdateVote($vote)
    {
        $voteExport = new VoteExport();

        $tokenNow = $vote['access_token'];
        $voteExport->resetAccessToken($vote['vote_guid']);
        $voteNew2 = $voteExport->getVote($vote['vote_guid']);
        $this->assertNotEquals($tokenNow, $voteNew2['access_token']);

        $data = array(
            'vote_guid' => 'xcvdf',
            'vote_allow_num' => '3',
            'vote_summary' => 'fuck you!',
            'access_token' => md5(time()),
        );
        $voteExport->updateVote($vote['vote_id'], $data);

        $voteNew = $voteExport->getVote($vote['vote_id']);
        $comp = array();
        foreach(array_keys($data) as $key) {
            $comp[$key] = $voteNew[$key];
        }
        $this->assertEquals(
            $data,
            $comp
        );
        return $voteNew;
    }

    /**
     * @depends testDependGetVote
     */
    function testDependDel($vote)
    {
        $voteExport = new VoteExport();
        $voteExport->forbiddenVote($vote['vote_id']);

        $vote = $voteExport->getVote($vote['vote_guid']);
        $this->assertEquals($vote['vote_status'], '0');

        $voteExport->delVote($vote['vote_guid']);
        $voteNew = $voteExport->getVote($vote['vote_id']);
        $this->assertFalse($voteNew);
    }
}
